﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace doveData.XmlScripts
{
    public class MarketXml : XmlHelper
    {
        public static string path;
        static List<object> list;

        public MarketXml()
        {
            SetXmlPath();
        }

        public void SetXmlPath()
        {
            path = "..//..//xml//marketInfo.txt";
        }

        public XElement LoadXML()
        {
            if (path == null)
                return null;
            XElement xml = XElement.Load(path);

            return xml;
        }

        public void AddXElement()
        {
            XElement xml = XElement.Load(path);

            string num = "";
            var query = from q in xml.Elements()
                        where
                        q.Attribute("id") != null
                        select q;
            foreach (var item in query)
            {
                num = item.Attribute("id").Value;
            }


            Console.WriteLine("\t\t\t\t\t请按要求输入数据：");
            Console.Write("\t\t\t\t\tid:");
            string id = Console.ReadLine();
            if (id != num) { id = (int.Parse(num) + 1).ToString(); }

            Console.Write("\t\t\t\t\tname:");
            string name = Console.ReadLine();

            Console.Write("\t\t\t\t\tx坐标:");
            string x = Console.ReadLine();

            Console.Write("\t\t\t\t\ty坐标:");
            string y = Console.ReadLine();

            Console.Write("\t\t\t\t\tgoods:");
            string goods = Console.ReadLine();

            Console.Write("\t\t\t\t\tscore:");
            string score = Console.ReadLine();

            XElement add = new XElement("market",
                new XAttribute("id", id),
                new XElement("name", name),
                new XElement("x", x),
                new XElement("y", y),
                new XElement("goods", goods),
                new XElement("score", score)
                );

            xml.Add(add);
            xml.Save(path);
            Console.WriteLine("\t\t\t\t\t增加成功！");
        }

        public void DeleteElement()
        {
            XElement xml = XElement.Load(path);
            Console.WriteLine("\t\t\t\t\t请输入您要删除的商店名：");
            Console.Write("\t\t\t\t\t");
            string market = Console.ReadLine();

            var query = from p in xml.Descendants("market") where p.Element("name").Value == market select p;

            var first = query.FirstOrDefault();

            if (first != null)
            {
                XElement findElement = first as XElement;
                findElement.Remove();
                xml.Save(path);

                ///summary
                ///删除操作后重置hotel节点的id属性值
                ///summary

                XMLOperate xmlOperate = new XMLOperate();
                xmlOperate.ResetID(xml, findElement);

                xml.Save(path);
                Console.WriteLine("\t\t\t\t\t删除成功！");
            }
            else { Console.WriteLine("\t\t\t\t\t抱歉，不存在这个商店！"); }
        }

        public void ModifyElement()
        {
            XDocument xml = XDocument.Load(path);

            Console.WriteLine("\t\t\t\t\t请输入您要修改的商店名：");
            Console.Write("\t\t\t\t\t");
            string market = Console.ReadLine();
            Console.WriteLine("\t\t\t\t\t该商店的name修改为：");
            Console.Write("\t\t\t\t\t");
            string newName = Console.ReadLine();
            Console.WriteLine("\t\t\t\t\t该商店的x坐标修改为：");
            Console.Write("\t\t\t\t\t");
            string newX = Console.ReadLine();
            Console.WriteLine("\t\t\t\t\t该商店的y坐标修改为：");
            Console.Write("\t\t\t\t\t");
            string newY = Console.ReadLine();
            Console.WriteLine("\t\t\t\t\t该商店的goods修改为：");
            Console.Write("\t\t\t\t\t");
            string newGoods = Console.ReadLine();
            Console.WriteLine("\t\t\t\t\t该商店的score修改为：");
            Console.Write("\t\t\t\t\t");
            string newScore = Console.ReadLine();

            var query = from p in xml.Descendants("market") where p.Element("name").Value == market select p;
            var first = query.FirstOrDefault();
            if (first != null)
            {
                XElement findElement = first as XElement;
                XElement xName = findElement.Element("name");
                XElement xX = findElement.Element("x");
                XElement yY = findElement.Element("y");
                XElement xGoods = findElement.Element("goods");
                XElement xScore = findElement.Element("score");
                xName.Value = newName;
                xX.Value = newX;
                yY.Value = newY;
                xGoods.Value = newGoods;
                xScore.Value = newScore;
                xml.Save(path);
                Console.WriteLine("\t\t\t\t\t修改成功！");
            }
            else { Console.WriteLine("抱歉，不存在这个商店！"); }
        }

        public void Output()
        {
            XElement myxml = LoadXML();
            IList<XElement> list = myxml.Elements().ToList();
            bool isThis = true;
            string id = "0";
            foreach (var item in (myxml.Elements().ToList().Elements().ToList()))
            {
                if (int.Parse(item.Parent.Attribute("id").Value) > int.Parse(id))
                {
                    isThis = true;
                    id = (int.Parse(id) + 1).ToString();
                }
                while (isThis)
                {
                    Console.WriteLine("\t\t\t\t\t第" + (int.Parse(item.Parent.Attribute("id").Value) + 1).ToString() + "家商店：");
                    Console.WriteLine("\t\t\t\t\tx坐标：" + item.Parent.Element("x").Value);
                    Console.WriteLine("\t\t\t\t\ty坐标：" + item.Parent.Element("y").Value);
                    isThis = false;
                }
                if (item.Name != "x" && item.Name != "y")
                {
                    Console.WriteLine("\t\t\t\t\t"+item.Name+":" + item.Value);
                }
            }
        }
    }
}
